package com.coder4j.starter.web.util;


import com.coder4j.starter.web.DataScopeEnums;
import com.coder4j.starter.web.jwt.AuthVO;

/**
 * 身份验证工具类
 * @author keller
 * @date 2024-4-17 00:52:16
 */
public class AuthUtils {

    /**
     * ThreadLocal 对象，存放当前线程的用户对象
     */
    private static ThreadLocal<AuthVO> authVO = new ThreadLocal<>();

    private static ThreadLocal<DataScopeEnums> dataScopeFlag = new ThreadLocal<>();

    public static void clear(){
        authVO.remove();
        dataScopeFlag.remove();
    }

    /**
     * 获取认证用户
     * @return
     */
    public static AuthVO getAuthVO(){
        return authVO.get();
    }

    /**
     * 设置认证用户，使用 AOP 的方式在进入每个Controller接口时获取 认证用户 ，并放入 ThreadLocal
     * @param vo
     */
    public static void setAuthVO(AuthVO vo){
        authVO.set(vo);
    }

    public static DataScopeEnums getDataScope(){
        return dataScopeFlag.get();
    }

    public static void setDataScope(){
        AuthVO vo = authVO.get();
        DataScopeEnums dataScope = DataScopeEnums.valueOf(vo.getDataScope());
        dataScopeFlag.set(dataScope);
    }
}
